from django.test import TestCase
from datetime import datetime
from users.models import User
from ..models import Tasks, Reward


class TsksTestClass(TestCase):

    def _create_rewards(self):
        reward_1 = Reward.objects.create(
            reward_name='Reward_1',
            slug='reward-1'
        )
        reward_2 = Reward.objects.create(
            reward_name='Reward_2',
            slug='reward-2'
        )
        self.reward_1 = reward_1
        self.reward_2 = reward_2
    
    def _create_tasks(self):
        user = User.objects.create(
            email='test@email.com'
        )
        task_1 =Tasks.objects.create(
            owner=user,
            title='test_title',
            description='test description',
            reward=self.reward_1
        )
        task_2 =Tasks.objects.create(
            owner=user,
            title='test2_title',
            description='test_2 description',
            year=str(datetime.now().year),
            reward=self.reward_2
        )
        task_3 =Tasks.objects.create(
            owner=user,
            title='test3_title',
            description='test_3 description'
        )
        self.task_1 = task_1
        self.task_2 = task_2
        self.task_3 = task_3

    def setUp(self):
        self._create_rewards()
        self._create_tasks()

    def test_task_title(self):
        self.assertEquals(self.task_1.title, 'test_title')

    def test_task_description(self):
        self.assertEquals(self.task_1.description, 'test description')
    
    def test_task_creation_date(self):
        creation = self.task_2.creation_date.strftime('%Y-%m-%d')
        self.assertEquals(creation, str(datetime.now()).split()[0])

    def test_task_owner(self):
        user = User.objects.get(id=1)
        self.assertEquals(user, self.task_1.owner)
    
    def test_task_count(self):
        tasks = Tasks.objects.all()
        self.assertTrue(tasks.exists())
        return self.assertEquals(tasks.count(), 3)
    
    def test_task_status_default(self):
        status = self.task_1.status
        return self.assertEquals(status, 'EM')

    def test_task_year_creation(self):
        now_year = str(datetime.now().year)
        return self.assertEquals(now_year, self.task_2.year)

    def test_task_reward_count(self):
        task_rewards = Tasks.objects.filter(reward__isnull=False)
        self.assertTrue(task_rewards.exists())
        task_rewards_count = task_rewards.count()
        return self.assertEquals(task_rewards_count, 2)
    
    def test_task_title_label(self):
        field_label = self.task_2._meta.get_field('title').verbose_name
        return self.assertEquals(field_label, 'Заголовок')

    def test_task_description_max_length(self):
        field_max_length = self.task_2._meta.get_field('description').max_length
        return self.assertEquals(field_max_length, 300)
    
    def test_task_isDone_default_value(self):
        return self.assertFalse(self.task_2.is_done)
    
    def test_task_isDone_count(self):
        self.task_3.is_done = True
        self.task_3.save()
        done_tasks = Tasks.objects.filter(is_done=True)
        self.assertTrue(done_tasks.exists())
        done_task_count = done_tasks.count()
        return self.assertEquals(done_task_count, 1)